import cart from '@/api/cart'

const state = {
    cartGoods: [],
    cartNum: 0,
    selectedGoods: [],
    selectedGoodsId: [],
    status: 'normal',
    isSelectedAll: false,
    totalPrice: 0,
    totalBonus: 0,
    requestSend: {
        cartNum: false
    }
}

const getters = {
    cartGoods: state => state.cartGoods,
    cartNum: state => state.cartNum,
    selectedGoods: state => state.selectedGoods,
    selectedGoodsId: state => state.selectedGoodsId,
    status: state => state.status,
    isSelectedAll: state => state.isSelectedAll,
    totalPrice: state => state.totalPrice,
    totalBonus: state => state.totalBonus

}

const actions = {
    // 加入购物车
    // 可传入goodsId 否则默认选择store/goods储存的goodsId
    async addGoods ({commit, state, rootState}, {goodsNum, goodsId}) {
        goodsId = goodsId || rootState.goods.goodsId
        const {data, success, msg} = await cart.addGoods({
            goodsId,
            goodsNum
        })
        if (!success) return {success, msg}
        commit('cartNum', state.cartNum + goodsNum)
        return {success}
    },
    // 获取购物车商品
    async getCartGoods ({commit}) {
        const {data} = await cart.getCartGoods()
        commit('cartGoods', data.shop_cart_info)
    },
    // 获取购物车数量
    async getCartNum ({state, commit}) {
        if (state.requestSend.cartNum) return
        const {data} = await cart.getCartNum()
        commit('cartNum', data.goods_count)
    },
    // 编辑数量
    async editCartNum ({commit}, {goods_id, num}) {
        const goods = await cart.editGoodsNum({goods_id, num})
        commit('edit', {num: goods.num, stores: goods.store, goods_id})
        commit('calculateTotal')
    },
    // 收藏
    collectGoods ({commit, state}) {
        const preGoodsIdArr = state.selectedGoods.map(g => g.pre_goods_id)
        cart.collectGoods({preGoodsIdArr, goodsIdArr: state.selectedGoodsId}).then(r => {
            commit('resetCart')
        })
    },
    // 移除
    removeGoods ({commit, state}) {
        cart.removeGoods({goodsIdArr: state.selectedGoodsId}).then(r => {
            commit('resetCart')
        })
    },
    // 点击选中商品
    toggleSelect ({commit}, {goods, isSelected}) {
        commit('toggleGoodsSelect', {goods, isSelected})
        commit('calculateTotal')
        commit('checkIsSelectedAll')
    },
    // 点击全部选中
    toggleSelectAll ({commit}) {
        commit('toggleGoodsSelectAll')
        commit('calculateTotal')
        commit('checkIsSelectedAll')
    }
}

const mutations = {
    // 获取购物车数据
    cartGoods (state, cartGoods) {
        state.cartGoods = cartGoods
    },
    // 获取购物车商品数量
    cartNum (state, num) {
        state.cartNum = Number(num)
        state.requestSend.cartNum = true
    },
    // 加减商品个数
    edit (state, {goods_id, num, stores}) {
        let goods = state.cartGoods.find(g => g.goods_id === goods_id)
        goods.goods_num = num
        goods.stores = stores
    },
    // 计算总价
    calculateTotal (state) {
        state.totalPrice = state.selectedGoods.reduce((p, c) => p + c.agent_price * c.goods_num, 0)
        state.totalBonus = state.selectedGoods.reduce((p, c) => p + c.bonus * c.goods_num, 0)
    },
    // 选中/取消选中商品
    toggleGoodsSelect (state, {goods, isSelected}) {
        if (isSelected) {
            state.selectedGoods.push(goods)
            state.selectedGoods.sort(g => g.goods_id)
            state.selectedGoodsId.push(goods.goods_id)
            state.selectedGoodsId.sort()
        } else {
            const index = state.selectedGoodsId.findIndex(id => id === goods.goods_id)
            state.selectedGoods.splice(index, 1)
            state.selectedGoodsId.splice(index, 1)
        }
    },
    // 全选/全不选
    toggleGoodsSelectAll (state) {
        state.isSelectedAll = !state.isSelectedAll
        state.selectedGoods = state.isSelectedAll ? [...state.cartGoods] : []
        state.selectedGoodsId = state.selectedGoods.map(g => g.goods_id)
    },
    // 是否处于全选状态
    checkIsSelectedAll (state) {
        state.isSelectedAll = state.selectedGoods.length === state.cartGoods.length
    },
    // 切换normal/edit状态
    toggleStatus (state, {status}) {
        state.status = status
    },
    resetCart (state) {
        state.cartGoods = state.cartGoods.filter(g => state.selectedGoodsId.indexOf(g.goods_id) === -1)
        state.selectedGoodsId = []
    }
}

export default {
    state,
    getters,
    actions,
    mutations,
    namespaced: true
}
